package com.ruoyi.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.gouhuoruku.domain.GouhuoRuku;
import com.ruoyi.gouhuoruku.service.IGouhuoRukuService;
import com.ruoyi.paiban.domain.*;
import com.ruoyi.paiban.service.IPaibanService;
import com.ruoyi.shangpinxinxi.domain.Shangpinxinxi;
import com.ruoyi.shangpinxinxi.service.IShangpinxinxiService;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku;
import com.ruoyi.xiaoshouchuku.domain.saomaChuku;
import com.ruoyi.xiaoshouchuku.domain.saomaChuku2Qianduan;
import com.ruoyi.xiaoshouchuku.domain.saomaTest;
import com.ruoyi.xiaoshouchuku.service.IXiaoshouChukuService;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/api")
public class mainController extends BaseController {

    @Autowired
    private IPaibanService paibanService;

    @Autowired
    private IXiaoshouChukuService xiaoshouChukuService;

    @Autowired
    private IShangpinxinxiService shangpinxinxiService;

    @Autowired
    private IGouhuoRukuService gouhuoRukuService;

    @PostMapping("/select2mainSearch")
    @ResponseBody
    private List<meiRiShuJu> select2mainSearch(String startDate, String endDate, String yewuyuan) throws ParseException {
        return paibanService.selectMeiRi2Xiaoshou(startDate, endDate, yewuyuan);
    }

    @PostMapping("/XiaoShouSearch2Main")
    @ResponseBody
    private List<xiaoShou2Main> XiaoShouSearch2Main(HttpServletRequest request) {
        List<xiaoShou2Main> list = paibanService.selectMeiRi2xiaoShou2Main();
        request.getSession().setAttribute("_xiaoshoumeizhou", list);
        return list;
    }

    @PostMapping("/XiaoShouSearch2New")
    @ResponseBody
    private List<xiaoShou2Main> XiaoShouSearch2New(HttpServletRequest request) {
        List<xiaoShou2Main> list = paibanService.selectMeiRi2XiaoShouSearch2New();
        request.getSession().setAttribute("_XiaoShouSearch2New", list);
        return list;
    }

    @PostMapping("/XiaoShouSearch2KuCun")
    @ResponseBody
    private List<kuCun2Main> XiaoShouSearch2KuCun(HttpServletRequest request) {
        List<kuCun2Main> list = paibanService.selectMeiRi2XiaoShouSearch2KuCun();
        request.getSession().setAttribute("_XiaoShouSearch2KuCun", list);
        return list;
    }

    @PostMapping("/QuanXian")
    @ResponseBody
    private String QuanXian(HttpServletRequest request) {
        return request.getSession().getAttribute("QuanXian").toString();
    }

    @PostMapping("/XiaoShouSearch2YiChuKu")
    @ResponseBody
    private List<XiaoshouChuku> XiaoShouSearch2YiChuKu(HttpServletRequest request) {
        String name = request.getSession().getAttribute("name4user").toString();
        return paibanService.selectXiaoShouSearch2YiChuKu(name);
    }

    @PostMapping("/XiaoShouSearch2WeiChuKu")
    @ResponseBody
    private List<XiaoshouChuku> XiaoShouSearch2WeiChuKu(HttpServletRequest request) {
        String name = request.getSession().getAttribute("name4user").toString();
        return paibanService.selectXiaoShouSearch2WeiChuKu(name);
    }

    @PostMapping("/XiaoShouChuKuSearch2WeiChuKu")
    @ResponseBody
    private List<XiaoshouChuku> XiaoShouChuKuSearch2WeiChuKu(HttpServletRequest request) {
        String name = request.getSession().getAttribute("name4user").toString();
        return paibanService.selectXiaoShouChuKuSearch2WeiChuKu(name);
    }

    @PostMapping("/qianDao2today")
    @ResponseBody
    private String qianDao2today(HttpServletRequest request) {
        String name = request.getSession().getAttribute("name4user").toString();
        Date date = new Date(System.currentTimeMillis());
        String dateNow = new SimpleDateFormat("yyyy-MM-dd").format(date);
        Paiban paiban = paibanService.selectPaibanBydate(dateNow);
        if (paiban != null && paiban.getYuangongName().equals(name)) {
            paiban.setQiandaoStatus("1");
            int a = paibanService.updatePaiban(paiban);
            if (a > 0) {
                return "1";
            }
        } else {
            return "-1";
        }
        return "-1";
    }

    @PostMapping("/paiBan2after")
    @ResponseBody
    private List<Paiban> paiBan2after() {
        Date date = new Date(System.currentTimeMillis());
        String dateNow = new SimpleDateFormat("yyyy-MM-dd").format(date);
        return paibanService.selectPaibanList2after(dateNow);
    }

    @PostMapping("/toTheMonth")
    @ResponseBody
    private List<meiRiShuJu> toTheMonth() {
        Date date = new Date(System.currentTimeMillis());
        String dateNow = new SimpleDateFormat("yyyy-MM").format(date) + "%";
        return paibanService.selectMeiRi2XiaoShouSearch4Month(dateNow);
    }


    @PostMapping("/Login2WeiXinFirst")
    @ResponseBody
    private String Login2WeiXinFirst(String username, String password, String code) throws IOException {
        if (username == null || password == null || code == null) {
            return null;
        }
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        //         请求客户端
        RestTemplate client = new RestTemplate();
        //      发起请求
        String body = client.getForEntity(url, String.class).getBody();
        System.out.println("******** Get请求 *********");
        assert body != null;
        System.out.println(body);
        url += "?appid=wx28262437646276b4";//自己的appid
        url += "&secret=8290dee3010f529addac16d6971ec941";//自己的appSecret
        url += "&js_code=" + code;
        url += "&grant_type=authorization_code";
        url += "&connect_redirect=1";
        String res = null;
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);    //GET方式
        CloseableHttpResponse response;
        // 配置信息
        RequestConfig requestConfig = RequestConfig.custom()          // 设置连接超时时间(单位毫秒)
                .setConnectTimeout(5000)                    // 设置请求超时时间(单位毫秒)
                .setConnectionRequestTimeout(5000)             // socket读写超时时间(单位毫秒)
                .setSocketTimeout(5000)                    // 设置是否允许重定向(默认为true)
                .setRedirectsEnabled(false).build();           // 将上面的配置信息 运用到这个Get请求里
        httpget.setConfig(requestConfig);                         // 由客户端执行(发送)Get请求
        response = httpClient.execute(httpget);                   // 从响应模型中获取响应实体
        HttpEntity responseEntity = response.getEntity();
        System.out.println("响应状态为:" + response.getStatusLine());
        if (responseEntity != null) {
            res = EntityUtils.toString(responseEntity);
            System.out.println("响应内容长度为:" + responseEntity.getContentLength());
            System.out.println("响应内容为:" + res);
        }
        // 释放资源
        httpClient.close();
        response.close();
        JSONObject jo = JSON.parseObject(res);
        String openid = jo.getString("openid");
        System.out.println("openid" + openid);

        boolean bo = paibanService.selectContainOpenID(openid);
        long uid = log(username, password);
        if (!bo) {
            if (uid == -1) {
                return "登录失败";
            } else {
                miniapp miniapp = new miniapp();
                miniapp.setUserid(Math.toIntExact(uid));
                miniapp.setOpenid(openid);
                List<miniapp> miniappList = paibanService.selectMiniapp(miniapp);
                if (miniappList == null) {
                    paibanService.insertMiniapp(miniapp);
                } else {
                    miniapp.setId(miniappList.get(0).getId());
                    paibanService.updataMiniapp(miniapp);
                }
                return String.valueOf(uid);
            }

        } else {
            return String.valueOf(uid);
        }
    }

    private long log(String username, String password) {
        String[] passlist = password.split("");
        String CharSelect = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String pass = "";
        List<User2Sys> userList = paibanService.selectUser2name(username);
        for (String s : passlist) {
            int charIndexOf = CharSelect.indexOf(s) - 11;
            if (charIndexOf < 0) {
                charIndexOf = CharSelect.length() + charIndexOf;
            }
            String uncode = String.valueOf(CharSelect.charAt(charIndexOf));
            //String uncode = String.valueOf(CharSelect.charAt(Integer.parseInt(passlist[i])-11));
            pass += uncode;
        }
        for (User2Sys userItem : userList) {
            String pwdMd5 = new Md5Hash(username + pass + userItem.getSalt()).toHex();
            if (pwdMd5.equals(userItem.getPassword())) {
                return userItem.getUserId();
            }
        }
        return -1;
    }

    @PostMapping("/bindLogin")
    @ResponseBody
    private long bindLogin(String username, String password) {
        long uid = log(username, password);
        if (uid > 0) return uid;
        return -1;
    }


    @PostMapping("/WeiZhiXing2WeiXin")
    @ResponseBody
    private List<XiaoshouChuku> WeiZhiXing2WeiXin(String userId) {
        String username = paibanService.selectUser4id(userId);
        XiaoshouChuku xiaoshouChuku = new XiaoshouChuku();
        xiaoshouChuku.setFahuoRen(username);
        xiaoshouChuku.setZhixing("未执行");
        List<XiaoshouChuku> xiaoshouChukuList = xiaoshouChukuService.selectXiaoshouChukuList(xiaoshouChuku);
        if (xiaoshouChukuList.size() != 0) {
            return xiaoshouChukuList;
        } else {
            return new ArrayList<>();
        }
    }

    @PostMapping("/ruKu2WeiXin")
    @ResponseBody
    private List<GouhuoRuku> ruKu2WeiXin(String userId) {
        String username = paibanService.selectUser4id(userId);
        if (username == null) return null;
        GouhuoRuku gouhuoRuku = new GouhuoRuku();
        gouhuoRuku.setRukuMen(username);
        gouhuoRuku.setZhiXing(0);
        List<GouhuoRuku> xiaoshouChukuList = gouhuoRukuService.selectGouhuoRukuList(gouhuoRuku);
        if (xiaoshouChukuList.size() != 0) {
            return xiaoshouChukuList;
        } else {
            return new ArrayList<>();
        }
    }

    @PostMapping("/saoMa2XiaoChengXu")
    @ResponseBody
    private String saoMa2XiaoChengXu(String code, String userId, String danhao) {
        code = code.replace("\n", "");
        Shangpinxinxi shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByDanhao(code);
        if (shangpinxinxi == null) {
            return "扫码错误，未找到对应的商品";
        }
        saomaChuku saomaChuku = new saomaChuku();
        saomaChuku.setChukuName(paibanService.selectUser4id(userId));
        java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
        saomaChuku.setChukuDate(date);

        XiaoshouChuku xiaoshouChuku = new XiaoshouChuku();
        xiaoshouChuku.setFahuoDanhao(danhao);
        List<XiaoshouChuku> xiaoshouChukuList = xiaoshouChukuService.selectXiaoshouChukuList(xiaoshouChuku);

        if (xiaoshouChukuList == null) {
            return "对应的出库单不存在或未录入";
        }

        if (shangpinxinxi.getShangpinName() == null) {
            shangpinxinxi.setShangpinName(shangpinxinxi.getShangpinLeibie());
        }
        int shutudo = xiaoshouChukuService.selectTodo2(shangpinxinxi.getShangpinBianhao(), xiaoshouChukuList.get(0).getChukuDanhao(), xiaoshouChukuList.get(0).getFahuoDanhao()) + 1;

        switch (shangpinxinxi.getShangpinLeibie()) {
            case "防火门（门扇）":
            case "双开防火门（左扇）":
                if (xiaoshouChukuList.get(0).getMen1Shuliang() == null) {
                    return "扫描的商品不在出库单上";
                }
                break;
            case "防火门（门框）":
                if (xiaoshouChukuList.get(0).getGoodNameKuang() == null) {
                    return "扫描的商品不在出库单上";
                }
                break;
            case "防火锁":
                if (xiaoshouChukuList.get(0).getShuliang1() == null || xiaoshouChukuList.get(0).getShuliang1().equals("")) {
                    return "扫描的商品不在出库单上";
                }
                break;
            case "闭门器":
                if (xiaoshouChukuList.get(0).getShuliang2() == null || xiaoshouChukuList.get(0).getShuliang2().equals("")) {
                    return "扫描的商品不在出库单上";
                }
                break;
            case "拉爆盖":
                if (xiaoshouChukuList.get(0).getShuliang3() == null || xiaoshouChukuList.get(0).getShuliang3().equals("")) {
                    return "扫描的商品不在出库单上";
                }
                break;
            case "双开防火门（右扇）":
                if (xiaoshouChukuList.get(0).getGoodNameMen2() == null) {
                    return "扫描的商品不在出库单上";
                }
                break;
            default:
                break;
        }

        if (shangpinxinxi.getShangpinLeibie().contains("门扇") ||
                shangpinxinxi.getShangpinLeibie().contains("左扇")) {
            if (xiaoshouChukuList.get(0).getMen1Shuliang() != null) {
                if (shutudo > Integer.parseInt(xiaoshouChukuList.get(0).getMen1Shuliang())) {
                    return "录入的门扇或整套门数量超过订单需求";
                }
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("右扇")) {
            if (xiaoshouChukuList.get(0).getMen2Shuliang() != null) {
                if (shutudo > Integer.parseInt(xiaoshouChukuList.get(0).getMen2Shuliang())) {
                    return "录入的右扇门数量超过订单需求";
                }
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("门框")) {
            if (xiaoshouChukuList.get(0).getKuangShuliang() != null) {
                if (shutudo > Integer.parseInt(xiaoshouChukuList.get(0).getKuangShuliang())) {
                    return "录入的门框数量超过订单需求";
                }
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("防火锁")) {
            if (xiaoshouChukuList.get(0).getShuliang1() != null) {
                if (shutudo > Integer.parseInt(xiaoshouChukuList.get(0).getShuliang1())) {
                    return "录入的防火锁数量超过订单需求";
                }
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("闭门器")) {
            if (xiaoshouChukuList.get(0).getShuliang2() != null) {
                if (shutudo > Integer.parseInt(xiaoshouChukuList.get(0).getShuliang2())) {
                    return "录入的闭门器数量超过订单需求";
                }
            }
        }
        if (shangpinxinxi.getShangpinLeibie().contains("拉爆盖")) {
            if (xiaoshouChukuList.get(0).getShuliang3() != null) {
                if (shutudo > Integer.parseInt(xiaoshouChukuList.get(0).getShuliang3())) {
                    return "录入的拉爆盖数量超过订单需求";
                }
            }
        }
        User2Sys user2Sys = paibanService.selectUser2Sys4id(Integer.parseInt(userId));
        saomaChuku.setChukuName(user2Sys.getUserName());
        saomaChuku.setChukuDanhao(xiaoshouChukuList.get(0).getChukuDanhao());
        saomaChuku.setFahuoDanhao(xiaoshouChukuList.get(0).getFahuoDanhao());
        saomaChuku.setShangpinBianhao(code);
        saomaChuku.setShangpinName(shangpinxinxi.getShangpinName());
        saomaChuku.setShangpinShuliang(1);

        HashMap<String, Integer> chuku = new HashMap<>();
        if (xiaoshouChukuList.get(0).getGoodNameMen1() != null && !xiaoshouChukuList.get(0).getGoodNameMen1().equals("")) {
            chuku.put(xiaoshouChukuList.get(0).getGoodNameMen1(), Integer.parseInt(xiaoshouChukuList.get(0).getMen1Shuliang()));
        }
        if (xiaoshouChukuList.get(0).getGoodNameMen2() != null && !xiaoshouChukuList.get(0).getGoodNameMen2().equals("")) {
            chuku.put(xiaoshouChukuList.get(0).getGoodNameMen2(), Integer.parseInt(xiaoshouChukuList.get(0).getMen2Shuliang()));
        }
        if (xiaoshouChukuList.get(0).getGoodNameKuang() != null && !xiaoshouChukuList.get(0).getGoodNameKuang().equals("")) {
            chuku.put(xiaoshouChukuList.get(0).getGoodNameKuang(), Integer.parseInt(xiaoshouChukuList.get(0).getKuangShuliang()));
        }
        if (xiaoshouChukuList.get(0).getShuliang1() != null && !xiaoshouChukuList.get(0).getShuliang1().equals("")) {
            chuku.put("防火锁", Integer.parseInt(xiaoshouChukuList.get(0).getShuliang1()));
        }
        if (xiaoshouChukuList.get(0).getShuliang2() != null && !xiaoshouChukuList.get(0).getShuliang2().equals("")) {
            chuku.put("闭门器", Integer.parseInt(xiaoshouChukuList.get(0).getShuliang2()));
        }
        if (xiaoshouChukuList.get(0).getShuliang3() != null && !xiaoshouChukuList.get(0).getShuliang3().equals("")) {
            chuku.put("拉爆盖", Integer.parseInt(xiaoshouChukuList.get(0).getShuliang3()));
        }

        //之前扫了多少商品
        List<saomaTest> saoma = xiaoshouChukuService.selectXiaoshouChukuByDanhao(xiaoshouChukuList.get(0).getFahuoDanhao());
        HashMap<String,Integer> saoma2New = new HashMap<>();
        for (saomaTest s : saoma) {
            if (s.getName().equals(shangpinxinxi.getShangpinName())) {
                s.setShuliang(String.valueOf(shutudo).split("\\.")[0]);
            }
            saoma2New.put(s.getName(), Integer.valueOf(s.getShuliang()));
        }

        chuku.forEach((key1, value1) -> {
            saoma2New.forEach((key2, value2) -> {
                if (key1.equals(key2)) {
                    if (value1 == value2) {
                        chuku.remove(key1,value1);
                    }
                }
            });
        });


        if (chuku.size() == 0) {
            xiaoshouChukuList.get(0).setZhixing("已执行");
            xiaoshouChukuService.updateXiaoshouChuku(xiaoshouChukuList.get(0));
            xiaoshouChukuService.insertSaomaChuku(saomaChuku);
            return "整单出库完成";
        }

        xiaoshouChukuService.insertSaomaChuku(saomaChuku);
        return "已成功出库";
    }

    @PostMapping("/ruKu2TiaoMaYuLan")
    @ResponseBody
    private String ruKu2TiaoMaYuLan(HttpServletRequest request, String id) {
        GouhuoRuku gouhuoRuku = new GouhuoRuku();
        gouhuoRuku.setGouhuoDanhao(id);
        List<GouhuoRuku> gouhuoRukuList = gouhuoRukuService.selectGouhuoRukuList(gouhuoRuku);
        if (gouhuoRukuList.size() == 0) return null;
        Shangpinxinxi shangpinxinxi = new Shangpinxinxi();
        shangpinxinxi.setShangpinName(gouhuoRukuList.get(0).getShangpinName());
        shangpinxinxi.setShangpinLeibie(gouhuoRukuList.get(0).getShangpinLeibie());
        shangpinxinxi.setKaiXiang(gouhuoRukuList.get(0).getKaiXiang());
        shangpinxinxi.setGuiGeKuan(gouhuoRukuList.get(0).getGuiGeKuan());
        shangpinxinxi.setGuiGeGao(gouhuoRukuList.get(0).getGuiGeGao());
        List<Shangpinxinxi> shangpinxinxinew = shangpinxinxiService.selectShangpinxinxiList(shangpinxinxi);
        if (shangpinxinxinew.size() == 0) return null;
        return shangpinxinxinew.get(0).getTiaoma();
    }

    @PostMapping("/UserInfoSelect")
    @ResponseBody
    private User2Sys UserInfoSelect(String userId) {
        long userid = Long.parseLong(userId);
        User2Sys user = paibanService.selectUser2Sys4id(userid);
        if (user.getUserId() != null) {
            XiaoshouChuku xiaoshouChuku = new XiaoshouChuku();
            xiaoshouChuku.setFahuoRen(user.getUserName());
            xiaoshouChuku.setZhixing("未执行");
            long count1 = xiaoshouChukuService.selectXiaoshouChukuList(xiaoshouChuku).size();
            GouhuoRuku gouhuoRuku = new GouhuoRuku();
            gouhuoRuku.setRukuMen(user.getUserName());
            gouhuoRuku.setZhiXing(0);
            long count2 = gouhuoRukuService.selectGouhuoRukuList(gouhuoRuku).size();
            user.setPassword(String.valueOf(count1));
            user.setSalt(String.valueOf(count2));
            return user;
        } else {
            return null;
        }
    }

    //    ruKuZhiXing
    @PostMapping("/ruKuZhiXing")
    @ResponseBody
    private String ruKuZhiXing(String danhao, String userid, String count) {
        String username = paibanService.selectUser4id(userid);
        if (username == null) {
            return "无此用户";
        }
        GouhuoRuku gouhuoRuku = new GouhuoRuku();
        gouhuoRuku.setGouhuoDanhao(danhao);
        gouhuoRuku.setRukuMen(username);
        gouhuoRuku.setZhiXing(0);
        List<GouhuoRuku> gouhuoRukuList = gouhuoRukuService.selectGouhuoRukuList(gouhuoRuku);
        if (gouhuoRukuList.size() == 0) {
            return "没有找到当前入库单";
        }
        gouhuoRuku = gouhuoRukuList.get(0);
        long shuliang = Long.parseLong(gouhuoRuku.getShuLiang());
        long rukushu = Long.parseLong(count);
        long yiruku = gouhuoRuku.getYiruku();
        if (shuliang - yiruku < rukushu) {
            return "入库数超过当前入库单数量";
        }
        gouhuoRuku.setYiruku(Math.toIntExact((rukushu + yiruku)));
        if (shuliang - yiruku == rukushu) {
            gouhuoRuku.setZhiXing(1);
            gouhuoRuku.setYiruku(null);
        }
        int tf = gouhuoRukuService.updateGouhuoRuku(gouhuoRuku);
        if (tf > 0) {
            return "入库更新成功";
        } else {
            return "入库更新失败";
        }

    }

    @PostMapping("/chukuDanList")
    @ResponseBody
    private List<saomaChuku> chukuDanList(String danhao) {
        if (danhao == null) return null;
        return xiaoshouChukuService.selectSaomaChuKu4DanGedanhao(danhao);
    }

    @PostMapping("/userInfoReValue")
    @ResponseBody
    private String userInfoReValue(String ReSet) throws IllegalAccessException {
        if (ReSet == null) return null;
        String[] relist = ReSet.split(",");
        String uid = relist[0].split("=")[1];
        String key = relist[1].split("=")[0];
        String value = relist[1].split("=")[1];

        User2Sys user2Sys = paibanService.selectUser2Sys4id(Long.parseLong(uid));

        if (relist.length > 2) {
            String value2pass = relist[2].split("=")[1];
            if (log(user2Sys.getLoginName(),value2pass) != -1) {
                String md5log = new Md5Hash(user2Sys.getLoginName() + value2pass + user2Sys.getSalt()).toHex();
                user2Sys.setPassword(md5log);
            }else {
                return "用户信息更新失败，用户名或密码错误";
            }
        }
        String usernameOld = user2Sys.getUserName();
        Field[] fields = user2Sys.getClass().getDeclaredFields();
        for (Field f : fields) {
            if (f.getName().equals(key) && !f.getName().equals("password") && !f.getName().equals("salt")) {
                f.setAccessible(true);
                f.set(user2Sys, value);
            }
        }

        //改用户名得更新出入库单据里的原有用户名
        if (!usernameOld.equals(user2Sys.getUserName())) {
            XiaoshouChuku xiaoshouChuku = new XiaoshouChuku();
            GouhuoRuku gouhuoRuku = new GouhuoRuku();
            xiaoshouChuku.setFahuoRen(user2Sys.getUserName());
            gouhuoRuku.setRukuMen(user2Sys.getUserName());
            saomaChuku saomaChuku = new saomaChuku();
            saomaChuku.setChukuName(user2Sys.getUserName());
            List<GouhuoRuku> gouhuoRukuList = gouhuoRukuService.selectGouhuoRukuList(gouhuoRuku);
            List<XiaoshouChuku> xiaoshouChukuList = xiaoshouChukuService.selectXiaoshouChukuList(xiaoshouChuku);
            List<saomaChuku> saomaChukuList = xiaoshouChukuService.selectSaomaChuKuList(saomaChuku);
            for (GouhuoRuku gr:gouhuoRukuList) {
                gr.setRukuMen(user2Sys.getUserName());
                gouhuoRukuService.updateGouhuoRuku(gr);
            }
            for (XiaoshouChuku xc:xiaoshouChukuList) {
                xc.setFahuoRen(user2Sys.getUserName());
                xiaoshouChukuService.updateXiaoshouChuku(xc);
            }
            for (saomaChuku sc:saomaChukuList) {
                sc.setChukuName(user2Sys.getUserName());
                xiaoshouChukuService.updateSaomaChuku(sc);
            }
        }
        int tf = paibanService.updateUser2Sys(user2Sys);
        if (tf > 0) {
            return "用户信息更新成功";
        } else {
            return "用户信息更新失败";
        }
    }

    @GetMapping("/itemReSetNum")
    @ResponseBody
    private String itemReSetNum(String fahuodanhao, String shangpinBianhao,String shangpinShuliang){
        if (shangpinShuliang == null || shangpinBianhao == null || fahuodanhao == null) {
            return "有修改参数为空";
        }
        for (char c : shangpinShuliang.toCharArray ()) {
            if (!Character.isDigit(c)) return "输入了非数字";
        }
        saomaChuku saomaChuku1 = xiaoshouChukuService.selectXiaosaomaTest4danhaoAndshangpin(fahuodanhao,shangpinBianhao);

        Shangpinxinxi shangpinxinxi = shangpinxinxiService.selectShangpinxinxiByDanhao(shangpinBianhao);

        saomaTest saomaChuku2 = xiaoshouChukuService.selectsaomaChuku2danhaoAndshangpin(shangpinBianhao,shangpinxinxi);

        int count = Integer.parseInt(shangpinShuliang);
        if (saomaChuku1 == null) {
            return "扫码记录不存在";
        }
        if (saomaChuku2 == null) {
            return "单据不出库该商品";
        }
        int count2 = Integer.parseInt(saomaChuku2.getShuliang());
        if (count > count2) {
            return "输入数量超过所需";
        }

        saomaChuku1.setShangpinShuliang(Integer.parseInt(shangpinShuliang));

        int bo = xiaoshouChukuService.updateSaomaChuku(saomaChuku1);

        if (bo > 0) {
            return "数量修改成功";
        }else {
            return "数量修改失败";
        }
    }
}
